package com.vm.activity;

import java.util.ArrayList;
import java.util.List;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.commons.VMConst;
import com.vm.db.GoodsCollectionDao;
import com.vm.model.GoodsCollection;
import com.vm.utils.MyDisplayImageOptions;
import com.wwtscapp.apk.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

@SuppressLint("InflateParams")
public class GoodsCollectionActivity extends Activity {
	ListView listview;
	ImageView imgbtn_search_back;
	private GoodsCollectionDao gDao = null;
	private List<GoodsCollection> listcollection;
	ImageView iv_nonecollection;
	TextView tv_all_number;
	List<String> goodsId = new ArrayList<String>();
	Button btn_delete;
	LinearLayout found_ll_1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		gDao = new GoodsCollectionDao(GoodsCollectionActivity.this);
		listcollection = gDao.selectall();
		if (listcollection != null) {
			if (listcollection.size() > 0) {
				setContentView(R.layout.goods_collection);
				imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
				imgbtn_search_back.setOnClickListener(btn_backListener);
				btn_delete = (Button) findViewById(R.id.btn_delete);
				btn_delete.setOnClickListener(delete_listener);
				tv_all_number = (TextView) findViewById(R.id.tv_all_number);
				tv_all_number.setText(Integer.toString(listcollection.size()));
				found_ll_1 = (LinearLayout) findViewById(R.id.found_ll_1);
				for (int i = 0; i < listcollection.size(); i++) {
					found_ll_1.addView(dyAddView(i, listcollection.get(i), 1));
				}
			} else {
				setContentView(R.layout.goods_collection_none);
				imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
				imgbtn_search_back.setOnClickListener(btn_backListener);
			}
		} else {
			setContentView(R.layout.goods_collection_none);
			imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
			imgbtn_search_back.setOnClickListener(btn_backListener);
		}
	}

	OnClickListener delete_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			for (int i = 0; i < goodsId.size(); i++) {
				String id = goodsId.get(i);
				gDao.delete(id);
				Intent intent = new Intent(GoodsCollectionActivity.this,
						GoodsCollectionActivity.class);
				startActivity(intent);
				finish();
			}
		}

	};
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// Log.i(TAG, "imgbtn_search_back返回按钮监听");
			onBackPressed();

		}
	};

	/**
	 * 返回按钮监听(手机自带返回按钮)
	 */
	@Override
	public void onBackPressed() {
		// Log.i(TAG, "返回按钮监听(手机自带返回按钮)");
		super.onBackPressed();
		// System.out.println("按下了back键   onBackPressed()");
	}

	public LinearLayout dyAddView(int i, final GoodsCollection goodsCollection,
			int progress) {
		LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.goods_collection_item, null);
		CheckBox checkbox_set = (CheckBox) linearLayout
				.findViewById(R.id.checkbox_set);
		ImageView iv_goodsImage = (ImageView) linearLayout
				.findViewById(R.id.iv_goodsImage);
		TextView tv_goodsName = (TextView) linearLayout
				.findViewById(R.id.tv_goodsName);
		TextView tv_goodsPrice = (TextView) linearLayout
				.findViewById(R.id.tv_goodsPrice);
		if (goodsCollection.getName() != null) {
			tv_goodsName.setText(goodsCollection.getName());
		}
		if (goodsCollection.getPrice() != null) {
			tv_goodsPrice.setText(Float.toString(goodsCollection.getPrice()));
		}
		ImageLoader.getInstance().displayImage(
				VMConst.URL + goodsCollection.getImageUrl(), iv_goodsImage,
				MyDisplayImageOptions.getInstance());
		if (goodsCollection.getGoodsId() != null) {
			tv_goodsName.setTag(goodsCollection.getGoodsId());
		}
		checkbox_set.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					goodsId.add(goodsCollection.getGoodsId());
					Log.e("list", Integer.toString(goodsId.size()));
				} else {
					goodsId.remove(goodsCollection.getGoodsId());
					Log.e("list", Integer.toString(goodsId.size()));
				}
			}
		});
		if (checkbox_set.isChecked()) {
			goodsId.add(goodsCollection.getGoodsId());
			Log.e("list", Integer.toString(goodsId.size()));
		}
		linearLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),
						GoodsDetailsActivity.class);
				intent.putExtra("goodsId", goodsCollection.getGoodsId());
				intent.putExtra("imageUrl", goodsCollection.getImageUrl());
				intent.putExtra("return", 1);
				v.getContext().startActivity(intent);
				finish();
			}
		});
		return linearLayout;
	}
}